<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>所有角色</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
     
      <el-button type="primary" @click="addAuthority">添加角色</el-button>

      <!-- 用户列表区域 -->
      <el-table
        :data="userlist"
        style="width: 100%; margin-bottom: 20px"
        row-key="authorityId"
        border
        default-expand-all
        :tree-props="{ children: 'children' }"
      >
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="authorityId" label="角色id" width="180">
        </el-table-column>
        <el-table-column prop="authorityName" label="角色名" width="180">
        </el-table-column>
        <el-table-column label="操作" width="360">
          <template slot-scope="scope">
           <!-- 添加按钮 -->
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="addUser(scope.row)">创建子角色</el-button>
            <!-- 修改按钮 -->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="updateUser(scope.row)">修改</el-button>
            <!-- 设置菜单 -->
            <el-button type="primary" icon="el-icon-setting" size="mini" @click="getMenu(scope.row)"></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deluser(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

        <!-- 抽屉 -->

        <el-drawer   title="设置菜单"   :visible.sync="drawer"    direction=rtl>
            <el-tree
                ref="tree"
                :data="menusList"
                show-checkbox
                node-key="ID"
                
                default-expand-all
                :props="defaultProps">
            </el-tree>
            <div class="card-bottom">
                <el-button type="primary" @click="setMenu" size="mini">确定</el-button>
            </div>
            
        </el-drawer>
    </el-card>
    <!-- 添加的对话框><-->
    <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogVisible"
        width="30%"
        >
        <!-- 主体区域-->
        <el-form ref="addUserformRef" :model="addUserform"  label-width="80px">
            <el-form-item label="父ID" prop="parentId" >
              <el-input v-model="addUserform.parentId" :disabled="disabled"></el-input>
            </el-form-item>
            <el-form-item label="角色ID" prop="authorityId">
              <el-input v-model="addUserform.authorityId"></el-input>
            </el-form-item>
            <el-form-item label="角色名" prop="authorityName">
              <el-input v-model="addUserform.authorityName"></el-input>
            </el-form-item>
        </el-form>
        <!-- 底部区域-->
        <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeUser">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>
<script >
export default {
  name: "authority",
  data() {
    return {
      userlist: [],
      menusList:[],
      drawer:false,
      defaultProps: {
          children: 'children',
          label: 'name'
        },
      row:null,
      menuForUser:[],
      query:null,
      dialogTitle:'',
      addUserform:{
        isUpdate:false,
        parentId:'',
        authorityId:'',
        authorityName:'',
      },
      disabled:false,
      dialogVisible:false,
    };
  },
  created(){
      
  },
  mounted() {
    this.getAuthorities();
  },
  methods: {
    async getAuthorities() {
      const { data: res } = await this.$http.get("/stu/getAuthority");
      if (res.code != 0) return this.$message.error("获取用户列表失败");
      this.userlist = res.data;
    },
    async getMenu(row){
      //打开抽屉
      this.drawer=true
      this.row=row
      this.menuForUser =await this.getMenuList(row)//包含1级和2级菜单
      this.menusList=await this.allMenus() //包含1级和2级菜单
      var ids=[]
      this.takeID(this.menuForUser,ids)
      //勾上 已经有的菜单
      console.log(ids)
      this.$refs.tree.setCheckedKeys(ids)
      
    },
    async allMenus(){
      const { data: res } = await this.$http.get("/stu/Menus");
      if (res.code != 0) return this.$message.error("获取菜单列表失败");
          return res.data
    },
    //獲取拥有的菜單
    async getMenuList(row) {
      var form = { authorityId: row.authorityId }
      const { data: res } = await this.$http.post("/stu/menuForUser", form)
     
      if (res.code !== 0) this.$message.error("获取菜单失败:" + res.msg)
      return res.data
    },
    //遍历提取ID
    takeID(menuForUser,arr){
        if (menuForUser!=null){
            for(var i=0;i<menuForUser.length;i++){
                if (menuForUser[i].children===null)
                arr.push( menuForUser[i].ID)

                this.takeID(menuForUser[i].children,arr)
            }
        }
    },
    async setMenu(){
        //每一个查找一下。我们只要children 不为空的
        var menus=this.$refs.tree.getCheckedNodes(false,true)
        
        var query={
            "authorityId":this.row.authorityId,
            "menu":menus
        }
        const { data: res } = await this.$http.post("/stu/setAuthMenu",query);
        if (res.code !== 0) return this.$message.error("设置菜单失败");
        return this.$message.success("设置菜单成功")
    },
    async deluser(row){
      const { data: res } = await this.$http.delete("/stu/deleteAuthority",{ data:{"authorityId":row.authorityId}});
      if (res.code !== 0) return this.$message.error("删除角色失败");
      this.getAuthorities()
      return this.$message.success("删除角色成功")
    },
    //更新角色id与角色名
    changeUser(){
    //效验参数是否有效哦
      this.$refs.addUserformRef.validate(async (valid)=>{
          if (!valid){ //无效
              return
          }
          var res 
          console.log(this.addUserform)
          if (this.addUserform.isUpdate==false){
            const {data:result}=await this.$http.post("/stu/createAuth",this.addUserform)
            res=result
          }else
          {
            const {data:result}=await this.$http.put("/stu/updateAuth",this.addUserform)
            res=result
          }
          if (res.code!==0) return this.$message.error("保存失败:"+res.msg); 
          console.log(res)
          this.$message.success("保存成功")
          this.getAuthorities()
      })
      this.dialogVisible = false
    },
    updateUser(row){
      console.log(row)
      this.disabled=true
      this.addUserform.parentId=row.parentId
      this.addUserform.authorityId=row.authorityId
      this.addUserform.authorityName=row.authorityName
      this.addUserform.isUpdate=true
      this.dialogVisible=true
    },
    addUser(row){
      this.disabled=true
      this.addUserform.parentId =row.authorityId
      this.addUserform.isUpdate=false
      this.dialogVisible=true
    },
    addAuthority(){
      this.disabled=true
      this.addUserform.parentId='0'
      this.addUserform.authorityId=null
      this.addUserform.authorityName=null
      this.addUserform.isUpdate=false
      this.dialogVisible=true
    }
  },
};
</script>
<style lang="less" scoped>
.el-breadcrumb {
  margin-bottom: 15px;
}
.el-card {
  box-shadow: 0 1px 1px;
}
.card-bottom{
    margin-bottom: 10px;
    padding-left: 40%;
}
.el-table{
  margin-top:10px;
}
</style>